import logging,os,time,yaml,csv
import random
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 日志配置
class LogUtils():

    def __init__(self, log_path,logger=None): # 初始化方法
        self.log_path = log_path # 日志文件将被保存的目录路径
        self.log_name1 = os.path.join(self.log_path, 'Dev-Test_%s.log' % time.strftime('%Y_%m_%d'))
        self.log_name2 = os.path.join(self.log_path, 'Test_%s.log' % time.strftime('%Y_%m_%d'))
        self.logger = logging.getLogger(logger)  #  创建一个日志器实例
        self.logger.setLevel(logging.INFO)  # 默认日志级别为：info

        self.fh1 = logging.FileHandler(self.log_name1, 'a', encoding='utf-8')  # 输出到文件
        self.fh1 .setLevel(logging.INFO) # log_name1 用于记录info级别日志
        self.fh2 = logging.FileHandler(self.log_name2, 'a', encoding='utf-8')  # 输出到文件
        self.fh2.setLevel(logging.ERROR) # log_name2 用于记录error级别日志

        # Formatter 定义了日志输出的格式
        formatter = logging.Formatter(
            # 时间、文件名、函数名、行、级别、信息内容：[2024-08-11 01:34:12,773] test_bbpc_UIauto.py->test_atags line:165 [INFO] : 状态码为：200
            '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s')
        self.fh1 .setFormatter(formatter) # 设置日志格式
        self.fh2.setFormatter(formatter)
        self.logger.addHandler(self.fh1) # 添加文件处理器：为了让日志器能够将日志消息输出到指定的文件中
        self.logger.addHandler(self.fh2)
        self.fh1.close() # 关闭文件处理器
        self.fh2.close()


   # 提供一个接口，使外部代码可以访问和使用配置好的日志器
    def get_log(self):
        return self.logger


# 读取Yaml文件
def read_Yaml(filepath):
    """"
    读取yaml配置文件
    :param filepath: yml文件路径
    :return python的字典
     """
    try:
        with open(filepath,encoding="gbk") as f:
            config = yaml.safe_load(f)
            return config
    # 解决问题“E   UnicodeDecodeError: 'gbk' codec can't decode byte 0x84 in position 88: illegal multibyte sequence”
    except UnicodeDecodeError:
        with open(filepath,encoding="utf-8") as f:
            config = yaml.safe_load(f)
            return config



# 读取csv文件方法
def get_CSVdata(csv_path): # 接受一个参数path，表示CSV文件的路径
    with open(csv_path,encoding="gbk") as file: # 打开文件
        lst = csv.reader(file) # 读取CSV文件的内容
        my_data = [] # 列表用于保存读取的内容
        next(lst) # 跳过首行
        for row in lst:
            my_data.append(row) # 读取的每一行数据添加到列表中
        return my_data


# 邮件配置
SMTP_SERVER = 'smtp.qq.com' # 指定连接该服务器来发送邮件
SMTP_PORT = 465  # QQ 邮箱推荐使用 SSL，因此使用端口 465
EMAIL_ADDRESS = '1046508182@qq.com' # 发件人
EMAIL_AUTH_CODE = 'pjuvbtrzqhmvbbeb'  # 从 QQ 邮箱生成的授权码（若改了qq密码，建议重新生成授权码避免收不到邮件）

# 发送邮件方法
def send_email(subject, message, to_emails): # 三个参数分别为：标题、主体内容、收件者
    msg = MIMEMultipart() # 用于构造邮件内容。
    # 设置邮件头部信息
    msg['From'] = EMAIL_ADDRESS  # 设置发件人
    msg['To'] = ', '.join(to_emails)  # 将收件人列表转换成逗号分隔的字符串
    msg['Subject'] = subject  # 设置邮件主题
    # 添加邮件正文
    msg.attach(MIMEText(message, 'plain')) # plain表示纯文本类型
    server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)    # 使用 SSL 连接到 SMTP 服务器
    server.login(EMAIL_ADDRESS, EMAIL_AUTH_CODE) #     # 登录到 SMTP 服务器
    text = msg.as_string()  # 将邮件转换成字符串格式，以便发送
    server.sendmail(EMAIL_ADDRESS, to_emails, text) # 发送邮件
    server.quit()  # 关闭 SMTP 连接


# 请求头部分用列表类型保存
headers_list = [
            {
                'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (iPad; CPU OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.109 Safari/537.36 CrKey/1.54.248666'
            }, {
                'user-agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320'
            }, {
                'user-agent': 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+'
            }, {
                'user-agent': 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 7.0; SM-G950U Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G965U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.1.0; SM-T837A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)'
            }, {
                'user-agent': 'Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 11; Pixel 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36'
            }, {
                'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
            }, {
                'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
            }, {
                'user-agent': 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1'
            }
        ]

# 随机取列表中的请求头
headers = random.choice(headers_list)